package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"time"
	"xorm.io/xorm"
)

/*
*
官网：https://xorm.io/zh/docs/chapter-06/1.lock/
*/
func main() {
	// 数据表对应实体类
	var (
		username = "root"
		password = "123asd"
		ip       = "localhost"
		port     = "3306"
		dbName   = "test"
		charset  = "utf8mb4"
	)
	// 数据源url: root:123asd@tcp(localhost:3306)/test?charset=utf8mb4
	datasource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s",
		username, password, ip, port, dbName, charset)

	// 创建 Engine 引擎，注意导入mysql驱动
	engine, err := xorm.NewEngine("mysql", datasource)
	if err != nil {
		return
	}

	// 1.0、同步数据库结构
	//err = engine.Sync(new(User))
	//if err != nil {
	//	fmt.Printf(err.Error())
	//	return
	//}

	// 1.1、InsertOne，参数一：影响条数；参数二：异常信息
	//_, _ = engine.InsertOne(&User{Id: 11111, Name: "张三", Age: 23, Pwd: "123456"})

	// 1.2、Insert，入参不限制，可以单个/多个/数组等
	// 一条数据
	//_, _ = engine.Insert(&User{Id: 11112, Name: "张三1", Age: 231, Pwd: "123456"})
	// 多条数据
	//_, _ = engine.Insert(
	//	&User{Id: 11113, Name: "张三2", Age: 232, Pwd: "123456"},
	//	&User{Id: 11114, Name: "张三3", Age: 233, Pwd: "123456"},
	//)
	// 切片
	//var users [] User
	//users = append(users, User{Id: 11115, Name: "张三4", Age: 234, Pwd: "123456"})
	//users = append(users, User{Id: 11116, Name: "张三5", Age: 235, Pwd: "123456"})
	//_, _ = engine.Insert(&users)

	// 2.0、更新Update
	//_, _ = engine.ID(11111).Update(User{Name: "李四"})

	// 3.0、删除
	//_, _ = engine.Delete(User{Id: 11111})

	// 4.0、原生sql语句执行(仅适用增、删、改的操作)
	//_, _ = engine.Exec("update user set name = ? where id = ?", "hello", 11112)

	// 5.0、查询,返回结果为map，一般不用
	//sql := "select * from user"
	//result1, _ := engine.Query(sql)
	//result2, _ := engine.QueryString(sql)
	//result3, _ := engine.QueryInterface(sql)

	// 5.1、查询单条，并封装到user对象中，等价于 `select * from user limit 1;`
	//user := User{}
	//_, _ = engine.Get(&user)
	//fmt.Println(user)

	// 5.2、根据条件查询单条，并封装到user对象中，
	//user := User{Name: "张三"}
	//_, _ = engine.Where("name = ?", user.Name).Asc("age").Get(&user)
	//fmt.Println(user)

	// 5.3、查询指定字段的值, cols中传入查询的字段
	//user := User{Id: 11112}
	//engine.Table(&user).Where("id = ?", user.Id).Cols("name", "pwd").Get(&user)
	//fmt.Println(user)

	// 5.4、count计数
	//count, _ := engine.Count(User{})
	//fmt.Println(count)

	// 5.5、查询多条记录
	//var users []User
	//engine.
	//	//Where("pwd = ?", User{Pwd: "123546"}).
	//	Limit(10, 0).
	//	Find(&users)
	//for _, user := range users {
	//	fmt.Println(user)
	//}

	// 5.6、Iterator循环，根据条件遍历结果，查询pwd=123456的数据
	//engine.Iterate(&User{Pwd: "123456"}, func(idx int, bean interface{}) error {
	//	fmt.Println(idx)	// 下标
	//	fmt.Println(bean.(*User))		// 取出每一行的值
	//	return err
	//})

	// 5.7、Rows循环，根据条件遍历结果，查询pwd=123456的数据
	//rows, _ := engine.Rows(&User{Pwd: "123456"})
	//defer rows.Close()
	//user := new(User)
	//for rows.Next() {
	//	rows.Scan(user)
	//	fmt.Println(user)
	//}

	// 6.0、事务
	session := engine.NewSession() // 创建一个session连接对象
	defer session.Close()          // 关闭session连接对象

	session.Begin() // 开始事务

	session.Commit() // 提交事务

	session.Rollback() // 回滚事务

}

type User struct {
	// 注意：首字母必须是大写，否则报错
	Id      int64
	Name    string
	Age     int
	Pwd     string    `xorm:"varchar(200)"`
	Created time.Time `xorm:"created"` // 约束，自动更新字段
	Updated time.Time `xorm:"updated"`
}
